<template>
	<view class="container">
		<uni-section title="车辆跟踪" type="line" padding>
			<u-steps :current="active" direction="column" :active-color="'#dd524d'">
				<u-steps-item v-for="(item, index) in stepsList" :key="index" :title="item.title" :desc="item.desc" />
			</u-steps>
		</uni-section>
		<uni-section title="磅房计重信息" type="line" padding>
			<view v-if="weighingInfo" class="weigh-table-vertical">
				<view class="weigh-row">
					<text class="weigh-label">毛重/KG</text>
					<text class="weigh-value">{{ weighingInfo.grossWeight }}</text>
				</view>
				<view class="weigh-row">
					<text class="weigh-label">皮重/KG</text>
					<text class="weigh-value">{{ weighingInfo.tareWeight }}</text>
				</view>
				<view class="weigh-row">
					<text class="weigh-label">净重/KG</text>
					<text class="weigh-value">{{ weighingInfo.netWeight }}</text>
				</view>
				<view class="weigh-row">
					<text class="weigh-label">
						{{ weighingInfo.goodCategoryId == 72 ? '折比/KG' : '扣重/KG' }}
					</text>
					<text class="weigh-value">{{ weighingInfo.deductWeight }}</text>
				</view>
				<view class="weigh-row">
					<text class="weigh-label">
						规格
					</text>
					<text class="weigh-value">
						{{
			  weighingInfo.goodId == 50 
				? (weighingInfo.spec ? weighingInfo.spec + '折95.5°' : '') 
				: weighingInfo.goodId == 52 
				  ? (weighingInfo.spec ? weighingInfo.spec + '折95°' : '') 
				  : weighingInfo.goodId == 52 
				? (weighingInfo.spec || '') 
				: '' 
			}}
					</text>
				</view>
				<view class="weigh-row">
					<text class="weigh-label">
						结算重量/吨
					</text>
					<text class="weigh-value">{{ (weighingInfo.weightBridge/1000).toFixed(3) }}</text>
				</view>
			</view>
			<view v-else style="color:#999;text-align:center;padding:20rpx;">暂无计重信息</view>
		</uni-section>
		
		
		<uni-section title="化验单信息" type="line" padding v-if="showLabSection">
			<!-- 食用酒精化验单 (goodsId === 50) -->
			<view v-if="labType === 'YJ' && (formEthanol || formMeasure)" class="lab-report-container">
				<view class="lab-header">
					<text class="lab-title">河南汉永酒精有限公司质量检验报告单</text>
				</view>
				
				<view class="lab-info-grid">
					<view class="info-row">
						<text class="info-label">产品名称</text>
					</view>
					<view class="info-row">
						<text class="info-label">食用酒精</text>
					</view>
					<view class="info-row">
						<text class="info-label">报告时间</text>
					</view>
					<view class="info-row">
						<text class="info-label">{{ year }}年{{ month }}月{{ day }}日</text>
					</view>
					<view class="info-row">
						<text class="info-label">检验标准</text>
					</view>
					<view class="info-row">
						<text class="info-label">GB/T 10343-2023 GB 31640-2016</text>
					</view>
					<view class="info-row">
						<text class="info-label">生产日期</text>
					</view>
					<view class="info-row">
						<text class="info-label">{{ year }}年{{ month }}月{{ (parseInt(day)-1).toString().padStart(2,'0') }}日</text>
					</view>
					<view class="info-row">
						<text class="info-label">数量基数</text>
					</view>
					<view class="info-row">
						<text class="info-label">40吨</text>
					</view>
					<view class="info-row">
						<text class="info-label">质量等级</text>
					</view>
					<view class="info-row">
						<text class="info-label">优级</text>
					</view>
					<view class="info-row">
						<text class="info-label">抽样数</text>
					</view>
					<view class="info-row">
						<text class="info-label">1000ml</text>
					</view>
					<view class="info-row">
						<text class="info-label">抽检人</text>
					</view>
					<view class="info-row">
						<text class="info-label">苗晓峰</text>
					</view>
					<view class="info-row">
						<text class="info-label">抽样时间</text>
					</view>
					<view class="info-row">
						<text class="info-label">{{ year }}年{{ month }}月{{ day }}日</text>
					</view>
					<view class="info-row">
						<text class="info-label">抽样地点</text>
					</view>
					<view class="info-row">
						<text class="info-label">酒库</text>
					</view>
				</view>

				<view class="lab-table">
					<view class="table-header">
						<text class="header-cell">检验项目</text>
						<text class="header-cell">标准要求</text>
						<text class="header-cell">检验结果</text>
						<text class="header-cell">单项判定</text>
					</view>
					<view class="table-row">
						<text class="cell-label">外观</text>
						<text class="cell-label">无色透明</text>
						<text class="cell-label">符合</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">气味</text>
						<text class="cell-label">具有乙醇固有香气，无异嗅</text>
						<text class="cell-label">符合</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">滋味</text>
						<text class="cell-label">纯净，微甜，无异味</text>
						<text class="cell-label">符合</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">酒精度/%vol ≥</text>
						<text class="cell-label">95.5</text>
						<text class="cell-label">{{ (weighingInfo && weighingInfo.spec) || (formMeasure && formMeasure.alcoholContent) || '95.5' }}</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">色度/号 ≤</text>
						<text class="cell-label">10</text>
						<text class="cell-label">10</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">硫酸实验色度/号 ≤</text>
						<text class="cell-label">10</text>
						<text class="cell-label">10</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">氧化时间/min ≥</text>
						<text class="cell-label">30</text>
						<text class="cell-label">{{ (formEthanol && formEthanol.oxidationTime) || '30' }}</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">醛(以乙醛计)(mg/L) ≤</text>
						<text class="cell-label">2</text>
						<text class="cell-label">{{ (formEthanol && formEthanol.aldehydes) || '2' }}</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">甲醇/(mg/L) ≤</text>
						<text class="cell-label">50</text>
						<text class="cell-label">{{ (formEthanol && formEthanol.methanol) || '50' }}</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">正丙醇/(mg/L) ≤</text>
						<text class="cell-label">12</text>
						<text class="cell-label">{{ (formEthanol && formEthanol.npropyl) || '12' }}</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">异丁醇+异戊醇/(mg/L) ≤</text>
						<text class="cell-label">2</text>
						<text class="cell-label">{{ (formEthanol && formEthanol.fusel) || '2' }}</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label" style="font-style: center;">酸(以乙酸计)/(mg/L) ≤</text>
						<text class="cell-label">10</text>
						<text class="cell-label">{{ (formEthanol && formEthanol.acidity) || '10' }}</text>
						<text class="cell-result">合格</text>
					</view>
				</view>

				<view class="lab-conclusion">
					<text class="conclusion-text">检验结论：所检项目符合GB/T 10343-2023、GB 31640-2016优级标准要求。</text>
					<text class="conclusion-company">河南汉永酒精有限公司质检中心</text>
					<text class="conclusion-date">{{ year }}年{{ month }}月{{ day }}日</text>
				</view>

				<view class="lab-footer">
					<view class="footer-row">
						<text class="footer-label">批号:</text>
					</view>
					<view class="footer-row">
						<text class="footer-value">{{ year.toString().slice(-2) }}{{ month }}{{ (parseInt(day)-1).toString().padStart(2,'0') }}{{ (weighingInfo && weighingInfo.tankNumber === '0') ? 'X' : 'J' }}</text>
					</view>
					<view class="footer-row">
						<text class="footer-label">车号:</text>
					</view>
					<view class="footer-row">
						<text class="footer-value">{{ (formItem && formItem.licensePlate) || '-' }}</text>
					</view>
				</view>
				<view class="signature-row">
					<text class="footer-label">签字:</text>
					<text class="signature-item">批准人: 张相瑞</text>
					<text class="signature-item">复核人: 李婷婷</text>
					<text class="signature-item">检验人: 刘培培</text>
				</view>
			</view>

			<!-- 无水乙醇化验单 (goodsId === 52) -->
			<view v-else-if="labType === 'WS' && formWS" class="lab-report-container ws-lab">
				<view class="lab-header">
					<text class="lab-title">河南省汉永酒精有限公司无水乙醇检验报告单</text>
				</view>
				
				<view class="lab-info-grid">
					<view class="info-row">
						<text class="info-label">检验标准</text>
					</view>
					<view class="info-row">
						<text class="info-label">GB/T 678-2023</text>
					</view>
					<view class="info-row">
						<text class="info-label">生产日期</text>
					</view>
					<view class="info-row">
						<text class="info-label">{{ year }}年{{ month }}月{{ day }}日</text>
					</view>
					<view class="info-row">
						<text class="info-label">检验日期</text>
					</view>
					<view class="info-row">
						<text class="info-label">{{ year }}年{{ month }}月{{ day }}日</text>
					</view>
					<view class="info-row">
						<text class="info-label">报告日期</text>
					</view>
					<view class="info-row">
						<text class="info-label">{{ year }}年{{ month }}月{{ day }}日</text>
					</view>
				</view>

				<view class="lab-table">
					<view class="table-header">
						<text class="header-cell">项目</text>
						<text class="header-cell">优级纯（GR）</text>
						<text class="header-cell">分析纯（AR）</text>
						<text class="header-cell">化学纯（CP）</text>
						<text class="header-cell">检验结果</text>
					</view>
					<view class="table-row">
						<text class="cell-label">性状</text>
						<text class="cell-standard-wide">无色透明液体，易挥发，能与水、三氯甲烷、乙醚等混合，易吸收</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">乙醇(CH₃CH₂OH)，w/%</text>
						<text class="cell-label">≥99.9</text>
						<text class="cell-label">≥99.7</text>
						<text class="cell-label">≥99.5</text>
						<text class="cell-result">99.92</text>
					</view>
					<view class="table-row">
						<text class="cell-label">密度(20°C)，ρ/(g/ml)</text>
						<text class="cell-label">0.789-0.791</text>
						<text class="cell-label">0.789-0.791</text>
						<text class="cell-label">0.789-0.791</text>
						<text class="cell-result">0.790</text>
					</view>
					<view class="table-row">
						<text class="cell-label">与水混合实验</text>
						<text class="cell-label">合格</text>
						<text class="cell-label">合格</text>
						<text class="cell-label">合格</text>
						<text class="cell-result">合格</text>
					</view>
					<view class="table-row">
						<text class="cell-label">蒸发残渣的质量分数/%</text>
						<text class="cell-label">≤0.0005</text>
						<text class="cell-label">≤0.001</text>
						<text class="cell-label">≤0.001</text>
						<text class="cell-result">0.0003</text>
					</view>
					<view class="table-row">
						<text class="cell-label">酸度(以H⁺计)/(mmol/g)</text>
						<text class="cell-label">≤0.0002</text>
						<text class="cell-label">≤0.0004</text>
						<text class="cell-label">≤0.001</text>
						<text class="cell-result">0.0001</text>
					</view>
					<view class="table-row">
						<text class="cell-label">碱度(以OH⁻计)/(mmol/g)</text>
						<text class="cell-label">≤0.00005</text>
						<text class="cell-label">≤0.0001</text>
						<text class="cell-label">≤0.0003</text>
						<text class="cell-result">0.00002</text>
					</view>
					<view class="table-row">
						<text class="cell-label">水分(H₂O)，w/%</text>
						<text class="cell-label">≤0.1</text>
						<text class="cell-label">≤0.3</text>
						<text class="cell-label">≤0.5</text>
						<text class="cell-result">0.05</text>
					</view>
					<view class="table-row">
						<text class="cell-label">甲醇(CH₃OH)，w/%</text>
						<text class="cell-label">≤0.005</text>
						<text class="cell-label">≤0.05</text>
						<text class="cell-label">≤0.2</text>
						<text class="cell-result">0.001</text>
					</view>
					<view class="table-row">
						<text class="cell-label">异丙醇[(CH₃)₂CHOH]，w/%</text>
						<text class="cell-label">≤0.003</text>
						<text class="cell-label">≤0.01</text>
						<text class="cell-label">≤0.05</text>
						<text class="cell-result">0.001</text>
					</view>
					<view class="table-row">
						<text class="cell-label">羰基化合物(以CO计)，w/%</text>
						<text class="cell-label">≤0.003</text>
						<text class="cell-label">≤0.003</text>
						<text class="cell-label">≤0.005</text>
						<text class="cell-result">0.002</text>
					</view>
					<view class="table-row">
						<text class="cell-label">还原高锰酸钾物质(以O计)，w/%</text>
						<text class="cell-label">≤0.00025</text>
						<text class="cell-label">≤0.00025</text>
						<text class="cell-label">≤0.0006</text>
						<text class="cell-result">0.00019</text>
					</view>
					<view class="table-row">
						<text class="cell-label">易炭化物质</text>
						<text class="cell-label">合格</text>
						<text class="cell-label">合格</text>
						<text class="cell-label">合格</text>
						<text class="cell-result">合格</text>
					</view>
				</view>

				<view class="lab-conclusion">
					<text class="conclusion-text">结论：符合优级纯标准</text>
				</view>

				<view class="lab-footer">
					<view class="signature-row">
						<text class="signature-item">批准人: 张相瑞</text>
						<text class="signature-item">复核人: 李婷婷</text>
						<text class="signature-item">检验人: 刘培培</text>
					</view>
				</view>
			</view>

			<view v-else style="color:#999;text-align:center;padding:20rpx;">暂无化验单数据</view>
		</uni-section>
	</view>
</template>

<script>
	import {
		getStatusValue,
		getPoundScaleWeighing,
		getPlanPriceItem,
		getEthanolYJInfo,
		getEthanolYJInfoByDate,
		getEthanolWSInfo,
		getEthanolWSInfoByDate,
	} from '@/api/goods/goods.js'
	export default {
		onLoad(options) {
			this.init(options)
		},
		data() {
			return {
				active: 0,
				stepsList: [

				],
				weighingInfo: null,
				// 化验单相关
				showLabSection: false,
				labType: '', // YJ: 食用酒精; WS: 无水乙醇
				goodsId: null,
				carStatus: null,
				year: new Date().getFullYear().toString().padStart(2, '0'),
				month: (new Date().getMonth() + 1).toString().padStart(2, '0'),
				day: new Date().getDate().toString().padStart(2, '0'),
				formItem: {},
				formEthanol: null,
				formMeasure: null,
				formWS: null,
			}
		},
		methods: {
			change() {
				if (this.active < this.stepsList.length - 1) {
					this.active += 1
				} else {
					this.active = 0
				}
			},
			init(options) {
				let params = {
					originalBillNumber: options.originalBillNumber,
					id: options.id
				}
				// 保存goodsId和carStatus用于条件判断
				this.goodsId = options.goodsId ? parseInt(options.goodsId) : null
				this.carStatus = options.carStatus ? parseInt(options.carStatus) : null

				getStatusValue(params).then(res => {
					if (res.code === 200) {
						this.stepsList = res.data.dataArr
						this.active = res.data.active
					}
				})
				getPoundScaleWeighing(params).then(res => {
					if (res.code === 200 && res.data.isWeighing === 1) {
						this.weighingInfo = res.data;
					} else {
						this.weighingInfo = null;
					}
				})

				console.log(this.goodsId);
				console.log(options.id);
				// 满足条件时加载化验单
				if ((this.goodsId === 50 || this.goodsId === 52) && options.id) {
					this.loadLabReport(options.id, this.goodsId)
				}
			},
			loadLabReport(detailId, goodsId) {
				// 先取定价单，依据定价日期决定是当天数据还是按日期查询
				getPlanPriceItem(detailId).then(response => {
					this.formItem = response.data || {}
					const pricingTime = this.formItem.pricingTime
					if (pricingTime) {
						const pricingDate = new Date(pricingTime)
						const today = new Date()
						const isToday = pricingDate.getFullYear() === today.getFullYear() &&
							pricingDate.getMonth() === today.getMonth() &&
							pricingDate.getDate() === today.getDate()
						if (!isToday) {
							this.year = pricingDate.getFullYear()
							this.month = (pricingDate.getMonth() + 1).toString().padStart(2, '0')
							this.day = pricingDate.getDate().toString().padStart(2, '0')
						}
						if (goodsId === 50) {
							this.labType = 'YJ'
							if (isToday) {
								getEthanolYJInfo().then(resp => {
									// 兼容返回结构
									this.formEthanol = resp.data && (resp.data.dataAlcoholLaboratory || resp.data)
									this.formMeasure = resp.data && (resp.data.dataMeasurementTeam || null)
									this.showLabSection = true
								})
							} else {
								getEthanolYJInfoByDate(pricingTime).then(resp => {
									this.formEthanol = resp.data && (resp.data.dataAlcoholLaboratory || resp.data)
									this.formMeasure = resp.data && (resp.data.dataMeasurementTeam || null)
									this.showLabSection = true
								})
							}
						} else if (goodsId === 52) {
							this.labType = 'WS'
							if (isToday) {
								getEthanolWSInfo().then(resp => {
									this.formWS = resp.data || null
									this.showLabSection = true
								})
							} else {
								getEthanolWSInfoByDate(pricingTime).then(resp => {
									this.formWS = resp.data || null
									this.showLabSection = true
								})
							}
						}
					} else {
						// 无定价时间，按当天查询
						if (goodsId === 50) {
							this.labType = 'YJ'
							getEthanolYJInfo().then(resp => {
								this.formEthanol = resp.data && (resp.data.dataAlcoholLaboratory || resp.data)
								this.formMeasure = resp.data && (resp.data.dataMeasurementTeam || null)
								this.showLabSection = true
							})
						} else if (goodsId === 52) {
							this.labType = 'WS'
							getEthanolWSInfo().then(resp => {
								this.formWS = resp.data || null
								this.showLabSection = true
							})
						}
					}
				}).catch(() => {
					// 即使定价单失败也不阻断页面
					this.showLabSection = false
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 30rpx;
		background-color: #ffffff;
		min-height: 100vh;
	}

	.button-container {
		display: flex;
		justify-content: center;
		margin: 40rpx 0;
	}

	.status-btn {
		width: 240rpx;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 40rpx;
		background-color: #dd524d;
		color: #ffffff;
		font-size: 28rpx;
		border: none;
		box-shadow: 0 4rpx 12rpx rgba(221, 82, 77, 0.2);

		&:active {
			transform: scale(0.98);
			opacity: 0.9;
		}
	}

	:deep(.u-steps-item) {
		margin-bottom: 20rpx;

		.u-steps-item__title {
			font-size: 28rpx;
			color: #333;
			font-weight: 500;
		}

		.u-steps-item__desc {
			font-size: 24rpx;
			color: #999;
			margin-top: 6rpx;
		}
	}

	.weigh-table-vertical {
		margin: 20rpx 0;
		border: 1px solid #eee;
		border-radius: 8rpx;
		overflow: hidden;
		font-size: 28rpx;
	}

	.weigh-row {
		display: flex;
		border-bottom: 1px solid #eee;

		&:last-child {
			border-bottom: none;
		}
	}

	.weigh-label {
		flex: 1;
		padding: 16rpx 0;
		text-align: center;
		border-right: 1px solid #eee;
		background: #f5f5f5;
		font-weight: bold;
		color: #333;
	}

	.weigh-value {
		flex: 1;
		padding: 16rpx 0;
		text-align: center;
		color: #666;
	}

	.lab-report-container {
		padding: 20rpx;
		background-color: #f9f9f9;
		border-radius: 10rpx;
		margin-top: 20rpx;
	}

	.lab-header {
		text-align: center;
		margin-bottom: 20rpx;
	}

	.lab-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.lab-info-grid {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(200rpx, 1fr));
		gap: 10rpx;
		margin-bottom: 20rpx;
	}

	.info-row {
		display: flex;
		justify-content: space-between;
		font-size: 28rpx;
		color: #555;
	}

	.info-label {
		font-weight: bold;
		color: #333;
	}

	.info-value {
		font-weight: bold;
		color: #dd524d;
	}

	.lab-table {
		border: 1px solid #ddd;
		border-radius: 8rpx;
		overflow: hidden;
		margin-bottom: 20rpx;
	}

	.table-header {
		display: grid;
		grid-template-columns: 1fr 1fr 1fr 1fr;
		background-color: #f0f0f0;
		font-weight: bold;
		color: #333;
		padding: 10rpx 0;
		text-align: center;
	}

	/* 无水乙醇表格5列布局 */
	.ws-lab .table-header,
	.ws-lab .table-row {
		grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
	}

	/* 跨列样式 - 让文本占据中间三列 */
	.cell-standard-wide {
		grid-column: 2 / 5; /* 从第2列开始，到第5列结束（即占据2、3、4列） */
		font-weight: bold;
		color: #000000;
		padding: 8rpx 0;
		text-align: center;
		border-right: 1px solid #eee;
	}

	.header-cell {
		padding: 8rpx 0;
		text-align: center;
		border-right: 1px solid #eee;
	}

	.header-cell:last-child {
		border-right: none;
	}

	.table-row {
		display: grid;
		grid-template-columns: 1fr 1fr 1fr 1fr;
		border-bottom: 1px solid #eee;
		padding: 10rpx 0;
		text-align: center;
	}

	.table-row:last-child {
		border-bottom: none;
	}

	.cell-label {
		font-weight: bold;
		color: #333;
		padding: 8rpx 0;
		text-align: center;
		border-right: 1px solid #eee;
	}

	.cell-label:last-child {
		border-right: none;
	}

	.cell-standard {
		font-weight: bold;
		color: #dd524d;
		padding: 8rpx 0;
		text-align: center;
		border-right: 1px solid #eee;
	}

	.cell-standard:last-child {
		border-right: none;
	}

	.cell-result {
		font-weight: bold;
		color: #4CAF50; /* 合格颜色 */
		padding: 8rpx 0;
		text-align: center;
		border-right: 1px solid #eee;
	}

	.cell-result:last-child {
		border-right: none;
	}

	.cell-judge {
		font-weight: bold;
		color: #dd524d; /* 不合格颜色 */
		padding: 8rpx 0;
		text-align: center;
	}

	.lab-conclusion {
		text-align: center;
		margin-bottom: 20rpx;
		padding: 10rpx 0;
		background-color: #e0f7fa;
		border: 1px solid #b2ebf2;
		border-radius: 8rpx;
	}

	.conclusion-text {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
		line-height: 1.5;
	}

	.conclusion-company {
		font-size: 24rpx;
		color: #666;
		margin-top: 10rpx;
	}

	.conclusion-date {
		font-size: 24rpx;
		color: #666;
		margin-top: 5rpx;
	}

	.lab-footer {
		display: flex;
		justify-content: space-around;
		align-items: center;
		padding: 10rpx 0;
		border-top: 1px solid #eee;
	}

	.footer-row {
		display: flex;
		justify-content: space-between;
		width: 100%;
		font-size: 24rpx;
		color: #555;
	}

	.footer-label {
		font-weight: bold;
		color: #333;
	}

	.footer-value {
		font-weight: bold;
		color: #dd524d;
	}

	.signature-row {
		display: flex;
		justify-content: space-around;
		margin-top: 10rpx;
		font-size: 24rpx;
		color: #555;
	}

	.signature-item {
		margin: 0 10rpx;
	}
</style>